﻿using LeaderCCSLaserUI.Models;
using LeaderCCSLaserUI.Services;
using MotionRobot.Models;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;

namespace LeaderCCSLaserUI.ViewModels.Dialogs.WorkPoint
{
    public class AddPointDialogViewModel : DialogViewModel
    {
        #region 变量
        private int handIndex = 0;
        #endregion
        #region 属性
        private string name;
        public string Name
        {
            get { return name; }
            set { SetProperty(ref name, value); }
        }
        private double x;
        public double X
        {
            get { return x; }
            set { SetProperty(ref x, value); }
        }
        private double y;
        public double Y
        {
            get { return y; }
            set { SetProperty(ref y, value); }
        }
        private double z;
        public double Z
        {
            get { return z; }
            set { SetProperty(ref z, value); }
        }
        private string alias;
        public string Alias
        {
            get { return alias; }
            set { SetProperty(ref alias, value); }
        }
        #endregion
        #region 方法绑定
        private DelegateCommand<object> operateCommad;
        public DelegateCommand<object> OperateCommad =>
            operateCommad ?? (operateCommad = new DelegateCommand<object>(ExecuteOperateCommad));
        private DelegateCommand getPosCommand;
        private readonly IGTSCardService gTSCard;

        public DelegateCommand GetPosCommand =>
            getPosCommand ?? (getPosCommand = new DelegateCommand(ExecuteGetPosCommand));

        void ExecuteGetPosCommand()
        {
            if (gTSCard.Connected)
            {
                X = Math.Round(GTSCard.GetEnc(handIndex == 0 ? gTSCard.A1 : gTSCard.A5), 3);
                Y = Math.Round(GTSCard.GetEnc(handIndex == 0 ? gTSCard.A2 : gTSCard.A6), 3);
                Z = Math.Round(GTSCard.GetEnc(handIndex == 0 ? gTSCard.A3 : gTSCard.A7), 3);
            }
        }
        void ExecuteOperateCommad(object obj)
        {
            switch (obj.ToString())
            {
                case "确认":
                    DialogParameters param = new DialogParameters();
                    param.Add("Name", Name);
                    param.Add("X", X);
                    param.Add("Y", Y);
                    param.Add("Z", Z);
                    param.Add("Alias", Alias);
                    OnRequestClose(new DialogResult(ButtonResult.Yes, param));
                    break;
                case "取消":
                    OnRequestClose(new DialogResult(ButtonResult.None));
                    break;
                default:
                    break;
            }
        }
        #endregion
        #region 导航

        public override void OnDialogOpened(IDialogParameters parameters)
        {
            handIndex = parameters.GetValue<int>("HandIndex");
            Name = parameters.GetValue<string>("Name");
            X = 0;
            Y = 0;
            Z = parameters.GetValue<double>("WeldZ");
            Alias = string.Empty;
        }
        #endregion
        #region 构造函数
        public AddPointDialogViewModel(IContainerProvider containerProvider) : base(containerProvider)
        {
            gTSCard = containerProvider.Resolve<IGTSCardService>("GTSCard");
        }
        #endregion
    }
}
